package com.example.easyutils.poetry;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.easyutils.BaseActivity;
import com.example.easyutils.R;
import com.example.easyutils.httputil.HttpGetRequest;
import com.example.easyutils.util.InputDel;
import com.example.easyutils.util.MD5Utils;
import com.example.easyutils.util.MyTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

public class PoetryActivity extends BaseActivity {
    private Map map;
    private List mList;
    private String poetryNum,poetryType,poetryYayun,errorTip = "出错啦";
    private Spinner poetryNumSpinner,poetryTypeSpinner,poetryYayunSpinner;
    private EditText poetryText;
    private TextView showPoetry;
    private static final int ERROR_CODE = 1,SUCCESS_CODE = 2;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SUCCESS_CODE:
                    Log.d("SUCCESS",map+"");
                    Long code = (Long) map.get("ret_code");
                    if(code!=0){
                        Toast.makeText(PoetryActivity.this,map.get("remark").toString(),Toast.LENGTH_LONG).show();
                        return;
                    }
                    mList = new ArrayList();
                    mList = (List) map.get("list");
                    String poetryStr = "";
                    for(int i = 0; i < mList.size(); i ++){
                        poetryStr += mList.get(i).toString()+"\n\n\n";
                    }

                    showPoetry.setText(poetryStr);
                    //获取剪贴板管理器：
                    ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
                    ClipData mClipData = ClipData.newPlainText("Label", poetryStr);
// 将ClipData内容放到系统剪贴板里。
                    cm.setPrimaryClip(mClipData);
                    break;
                case ERROR_CODE:

                    Toast.makeText(PoetryActivity.this,errorTip,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.poetry_layout);
        poetryNumSpinner = (Spinner)findViewById(R.id.poetryNum);
        poetryTypeSpinner = (Spinner)findViewById(R.id.poetryType);
        poetryYayunSpinner = (Spinner)findViewById(R.id.poetryYayun);
        poetryText = (EditText)findViewById(R.id.poetryText);
        showPoetry = (TextView)findViewById(R.id.showPoetry);

        ImageView imageView = null;
        InputDel inputDel = new InputDel(poetryText,imageView,PoetryActivity.this);
        inputDel.initListener();
        inputDel.editDel();
    }

    private void getSpinnerValue(String str1,String str2,String str3){
        switch (str1){
            case "五言":
                poetryNum = "5";
                break;
            case "七言":
                poetryNum = "7";
                break;
        }
        switch (str2){
            case "藏头":
                poetryType = "1";
                break;
            case "藏尾":
                poetryType = "2";
                break;
            case "藏中":
                poetryType = "3";
                break;
            case "递增":
                poetryType = "4";
                break;
            case "递减":
                poetryType = "5";
                break;
        }
        switch (str3){
            case "双句一压":
                poetryYayun = "1";
                break;
            case "双句押韵":
                poetryYayun = "2";
                break;
            case "一三四押":
                poetryYayun = "3";
                break;
        }
    }

    public void getPoetry(View v){//获取诗
        getSpinnerValue((String)poetryNumSpinner.getSelectedItem(),(String)poetryTypeSpinner.getSelectedItem(),
                (String)poetryYayunSpinner.getSelectedItem());
        String keyStr = poetryText.getText().toString();
        //        Log.d("MD5",MD5Utils.digest("key毛扬能num4showapi_appid400982showapi_timestamptype202010211500002type2yayuntype218f9632e5e7e4c14b187790131ae8023"));
        String md5Str = "key"+keyStr+"num"+poetryNum+"showapi_appid401870"+"type"+poetryType+"yayuntype"+poetryYayun+"884da662c86740f888259c43150ffbdd";
        String MD5 = MD5Utils.digest(md5Str);

        HttpGetRequest.sendOkHttpGetRequst("https://route.showapi.com/950-1?key="+keyStr+"&num="+poetryNum+"&showapi_appid=401870"+"&type="+poetryType+
                        "&yayuntype="+poetryYayun+"&showapi_sign="+MD5, new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.sendEmptyMessage(ERROR_CODE);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.d("SUCCESS_CO",result);
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(new TypeToken<Map<String, Object>>(){

                        }.getType(),new MyTypeAdapter()).create();
                Map<String,Object> dataDetail = gson.fromJson(result,new TypeToken<Map<String,Object>>(){

                }.getType());

                Long code = (Long) dataDetail.get("showapi_res_code");
                if(code==0){
                    map = new HashMap();
                    map = (Map) dataDetail.get("showapi_res_body");
                    handler.sendEmptyMessage(SUCCESS_CODE);
                }else{
                    errorTip = (String) dataDetail.get("showapi_res_error");
                    handler.sendEmptyMessage(ERROR_CODE);
                }
//                ret_code
            }
        });
    }
}
